/*
 * Copyright (c) Jan Šlahora (${USER}) - 2011.
 * Created in IntelliJ IDEA 10.5
 */

package MW.CetnostiTaby;

import MW.MWClasses;

import javax.swing.*;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;

public class TabComponentsDemo extends JPanel{
  private final JTabbedPane pane;

  public TabComponentsDemo() {
    // Set System L&F
    try {
      UIManager.setLookAndFeel(
          UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
      System.err.println("Error when setting Look and Feel");
      JOptionPane.showMessageDialog(null, "Error when setting Look and Feel", "Error", JOptionPane.ERROR_MESSAGE);
    }
    pane = new JTabbedPane();
    add(pane);

  }

    private String[] dejJmena(int c){
      String[] jmena = new String[c];
      for (int i = 0; i < c; i++) {
        jmena[i] = pane.getTitleAt(i);
      }
      return jmena;
  }

  private Component[] dejKomponenty(int c) {
    Component[] x = new Component[c];
    for (int i = 0; i < c; i++) {
      x[i] = pane.getComponentAt(i);
    }
    return x;
  }

  public ArrayList<String> dejTexty(int c) {
    ArrayList<String> texty = new ArrayList<>();
    Component[] x = dejKomponenty(c);
    for (int i = 0; i < c; i++) {
      JViewport jv = ((JScrollPane) x[i]).getViewport();
      if (jv.getComponent(0) instanceof JEditorPane) {
        texty.add(((JEditorPane) jv.getComponent(0)).getText());
      }
    }
    return texty;
  }

  public HashMap<String, String> dejTextyHM(int c) {
    HashMap<String, String> texty = new HashMap<>();
    Component[] x = dejKomponenty(c);
    for (int i = 0; i < c; i++) {
      if (x[i] instanceof JScrollPane) {
        JViewport jv = ((JScrollPane) x[i]).getViewport();
        if (jv.getComponent(0) instanceof JEditorPane) {
          String titulek = pane.getTitleAt(i);
          while (texty.containsKey(titulek)) {
            titulek = (String) JOptionPane.showInputDialog(this, "Je třeba zadat unikátní jméno textu:", "Změna názvu", JOptionPane.WARNING_MESSAGE, null, null, pane.getTitleAt(i));
            pane.setTitleAt(i, titulek);
          }

          texty.put(pane.getTitleAt(i), ((JEditorPane) jv.getComponent(0)).getText());
        }
      }
    }
    return texty;
  }

  public void runTest() {
    ChangeListener chL = pane.getChangeListeners()[0];
    pane.removeChangeListener(chL);
    int c = pane.getTabCount();
    String[] jmena = dejJmena(c);
    Component[] comps = dejKomponenty(c);
    pane.removeAll();

    for (int i = 0; i < c; i++) {
      
      pane.add(jmena[i], comps[i]);
      initTabComponent(i);
    }

    pane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
    // setSize(new Dimension(400, 200));
    setVisible(true);
    pane.addChangeListener(chL);
  }

  public void runTest(int i) {
    ChangeListener chL = pane.getChangeListeners()[0];
    pane.removeChangeListener(chL);
    //pane.add(pane.getTitleAt(i), pane.getComponent(i));
    initTabComponent(i);

    pane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);

    setVisible(true);
    pane.addChangeListener(chL);
  }

  private void initTabComponent(int i) {
    pane.setTabComponentAt(i,
        new ButtonTabComponent(pane));
  }

  public JTabbedPane getPane() {
    return pane;
  }

  public void odstranTab(int index) {
    pane.remove(index);
  }

  public void odstranTaby() {
     pane.removeAll();
  }

  public void prejmenujTab(String jmeno) {
    String title = MWClasses.dejJmeno(jmeno);
    if (title == null) {
      title = "Text " + pane.getTabCount();
    }
    int index = pane.getSelectedIndex();
    Component c = pane.getSelectedComponent();
    c.setName(title);
    odstranTab(index);
    pane.add(c, title, index);
    runTest();
    pane.setSelectedIndex(index);
  }
}

